package com.leetcode.merci.lc476;

/**
 * Created by bowen on 2017/7/24.
 */
public class Solution {
    public int findComplement(int num) {
        int flag=0;
        for(int i=0;i<32;i++)
        {
            if((num>>i|0)==0){
                flag=i;
                break;
            }
        }
        int result = ~num;
        int sum=0;
        int bin=1;
        for(int i=0;i<flag;i++){
            if((result>>i&1)==1){
                sum+=bin;
            }
            bin*=2;
        }
        return sum;
    }
}